<?php
//
// +--------------------------------------+
// |PHP version 5.5
// +--------------------------------------+
// |知果汇后台登录模块
// +--------------------------------------+
// |2017-12-26
// +--------------------------------------+
// |潘龙兵
// +--------------------------------------+
// |知果汇前台
// +--------------------------------------+
//
namespace app\api\controller;

use think\View;
use think\Controller;
use think\Db;
use app\data\Data;
use app\api\model\Common;
use think\Config;

// use app\api\model\Common;
class Dictionary extends Index
{
    /*
    * @知果汇后台
    * @字典
    * @2018-02-05
    * @潘龙兵
    */
    public function index()
    {
        $zgh_dictionary = new Data('dictionary');
        $where['tag'] = 0;
        $where['status'] = array('neq', '0');
        $where['tid'] = input('get.tid');
        $res = $zgh_dictionary->selectAll($where);
        // dump($res);exit;
        $data = $this->getdata($res);
        if (isset($data)) $this->_jsonp('200', '获取成功', $data);
        $this->_jsonp('1000', '获取失败', null);
        // dump($a);exit;
    }


    /*
    * @知果汇后台
    * @字典模块列表
    * @2018-02-08
    * @潘龙兵
    */
    public function model()
    {
        $zgh_dictionary = new Data('dictionary');
        $where['status'] = array('neq', '0');
        $where['pid'] = 0;
        $res = $zgh_dictionary->selectAll($where);
        if (isset($res)) $this->_jsonp('200', '获取成功', $res);
        $this->_jsonp('1000', '获取失败', null);
    }


    /*
    * @知果汇后台
    * @获取最小级数据
    * @2018-02-06
    * @潘龙兵
    */
    public function d_list()
    {
        if (!isset($_GET['id'])) $this->_jsonp("1004", "参数错误", null);
        $id = input('get.id');
        $page = 1;
        $page = !empty(input("get.page")) ? input("get.page") : $page;
        $where['d.pid'] = $id;
        $where['d.status'] = array('neq', '0');
        $join[0] = '__D_TYPE__ t';
        $join[1] = 't.id = d.tid';
        $alias = 'd';
        $zgh_dictionary = new Data('dictionary');
        // dump($count);exit;
        $field = 'd.id,d.name,d.pid,d.status,d.time,d.tid,t.name as t_name';
        $res = $zgh_dictionary->joinPage($page, $join, $alias, $where, $field);

        $data['res'] = $res;
        $data['count'] = $zgh_dictionary->countJion($where, $join, $alias);

        // dump($res);exit;
        if (isset($res)) $this->_jsonp('200', '获取成功', $data);
        $this->_jsonp('1000', '获取失败', null);
    }


    /*
    * @知果汇后台
    * @添加字典
    * @2018-02-06
    * @潘龙兵
    */
    public function add_model()
    {
        if (!isset($_GET['name'])) $this->_jsonp("1004", "参数错误", null);
        $name = input('get.name');
        $t['name'] = $name;
        $t['time'] = time();
        $d_type = new Data('d_type');
        $type_id = $d_type->insertData($t);
        // dump($type_id);exit;
        if (!isset($type_id)) $this->_jsonp('1000', '添加失败', null);
        $d['name'] = $name;
        $d['pid'] = 0;
        $d['time'] = time();
        $d['tag'] = 0;
        $d['tid'] = $type_id;
        $zgh_dictionary = new Data('dictionary');
        $res = $zgh_dictionary->insertData($d);
        if (isset($res)) {
            action_log('添加字典','log_admin',1);
            $this->_jsonp('200', '添加成功', $d);
        } else {
            action_log('添加字典','log_admin');
            $this->_jsonp('1000', '添加失败', null);
        }
    }


    /*
    * @知果汇后台
    * @添加字典
    * @2018-02-06
    * @潘龙兵
    */
    public function add()
    {
        if (!isset($_GET['name']) || !isset($_GET['pid']) || !isset($_GET['tag'])) $this->_jsonp("1004", "参数错误", null);
        $data['name'] = input('get.name');
        $data['pid'] = input('get.pid');
        $data['time'] = time();
        $data['tag'] = input('get.tag');
        $data['tid'] = input('get.tid');
        $zgh_dictionary = new Data('dictionary');
        $res = $zgh_dictionary->insertData($data);
        // dump($res);exit;
        if (isset($res)) {
            action_log('添加字典', 'log_admin', 1);
            $this->_jsonp('200', '添加成功', null);
        } else {
            action_log('添加字典', 'log_admin');
            $this->_jsonp('1000', '添加失败', null);
        }
    }


    /*function myfun($id)
    {
      $sql = "select `path` from `table` ";
      $rs = mysql_query($sql);
      while($rs =mysql_fetch_array($rs)){
        $arr[]=explode('-',$rs);
      }
      $sql = "select $id from `table` ";
      $rs = mysql_query($sql);
      foreach($arr as $val){
        if($val==mysql_fetch_array($rs)){
            echo $val;
        }
      }
    }
  // 以上不是递归做的 以下递归写的
    function myfun($id)
    {
      $sql = "select `pid` from `table` ";
      $rs = mysql_query($sql);
      while($rs =mysql_fetch_array($rs)){
        $arr[]=$rs;
      }
    }
    function myfun_2($id){
    if($id<=count($arr)){
     foreach($arr as $val){
        if($val!=$id){
            echo $id_1.=myfun_1($id);
        }else{
                $id++;}
        }
        return $id_1; }
    }*/

    /*
    * @知果汇后台
    * @获取当前那么
    * @2018-02-06
    * @潘龙兵
    */
    public function get_info()
    {
        if (!isset($_GET['id'])) $this->_jsonp("1004", "参数错误", null);
        $id = $_GET['id'];
        $join[0] = '__D_TYPE__ t';
        $join[1] = 't.id = d.tid';
        $alias = 'd';
        $field = 'd.id,d.name,d.pid,d.status,d.time,d.tid,t.name as t_name';
        $where['d.id'] = $id;
        $zgh_dictionary = new Data('dictionary');
        $res = $zgh_dictionary->joinOne($join, $where, $field, $alias);
        // dump($res);exit;
        if (!isset($res)) $this->_jsonp('1000', '查询失败', null);
        $where_n['id'] = $res['pid'];
        $res_name = $zgh_dictionary->_find($where_n, 'name');
        if (!isset($res_name)) {
            $res['up_name'] = '';
        } else {
            $res['up_name'] = $res_name['name'];
        }

        $this->_jsonp('200', '获取成功', $res);
    }


    /*
    * @知果汇后台
    * @改变状态（0：删除；1：激活；2：禁用）
    * @2018-02-06
    * @潘龙兵
    */
    public function change()
    {
        if (!isset($_GET['id']) || !isset($_GET['status'])) $this->_jsonp("1004", "参数错误", null);
        $id = input('get.id');
        $where['id'] = $id;
        $data['status'] = input('get.status');
        $zgh_dictionary = new Data('dictionary');
        $res = $zgh_dictionary->_update($data, $where);
        // dump($res);exit;
        if ($res !== false) {
            action_log('数据字典修改状态', 'log_admin', 1);
            $this->_jsonp('200', '修改成功', null);
        } else {
            action_log('数据字典修改状态', 'log_admin');
            $this->_jsonp('1000', '修改失败', null);
        }
    }


    /*
    * @知果汇后台
    * @批量删除
    * @2018-02-06
    * @潘龙兵
    */
    public function batch_del()
    {
        if (!isset($_GET['id'])) $this->_jsonp("1004", "参数错误", null);
        $id = input('get.id');
        $where['id'] = array('in', "$id");
        $data['status'] = 0;
        $zgh_dictionary = new Data('dictionary');
        $res = $zgh_dictionary->_update($data, $where);
        // dump($res);exit;
        if ($res !== false) {
            action_log('数据字典删除', 'log_admin', 1);
            $this->_jsonp('200', '删除成功', null);
        } else {
            action_log('数据字典删除', 'log_admin');
            $this->_jsonp('1000', '删除失败', null);
        }
    }


    /*
    * @知果汇后台
    * @编辑
    * @2018-02-06
    * @潘龙兵
    */
    public function edit()
    {
        if (!isset($_GET['id']) || !isset($_GET['name'])) $this->_jsonp("1004", "参数错误", null);
        $where['id'] = input('get.id');
        $data['name'] = input('get.name');
        $zgh_dictionary = new Data('dictionary');
        $res = $zgh_dictionary->_update($data, $where);
        // dump($res);exit;
        if ($res !== false) {
            action_log('数据字典编辑', 'log_admin', 1);
            $this->_jsonp('200', '修改成功', null);
        } else {
            $this->_jsonp('1000', '修改失败', null);
            action_log('数据字典编辑', 'log_admin');
        }
    }


    /*
    * @知果汇后台
    * @递归
    * @2018-02-06
    * @潘龙兵
    */
    public function getdata($array, $pid = 0)
    {
        $arr = array();
        $temp = array();
        foreach ($array as $v) {
            if ($v['pid'] == $pid) {
                $temp = $this->getdata($array, $v['id']);
                //判断是否存在子数组
                if ($temp) {
                    $v['son'] = $temp;
                } else {
                    $v['son'] = null;
                }
                $arr[] = $v;
            }
        }
        return $arr;
    }


}
